package com.srp.blockchain.bcos.controller;

import com.google.gson.Gson;
import com.srp.blockchain.fisco.bcos.contract.HelloWorld;
import com.srp.common.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.fisco.bcos.web3j.crypto.Credentials;
import org.fisco.bcos.web3j.protocol.Web3j;
import org.fisco.bcos.web3j.tx.gas.StaticGasProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigInteger;
import java.util.Objects;

@Slf4j
@Api(tags = "测试智能合约")
@RestController
@RequestMapping("/testContract")
public class HelloController {

    @Autowired
    private Web3j web3j;
    @Autowired
    private Credentials credentials;

    /**
     * 存证合约测试
     * @return
     */

//    public static EvidenceController load(String contractAddress, Client client, CryptoKeyPair credential) {
//        return new EvidenceController(contractAddress, client, credential);
//    }


    @ApiOperation("测试方法")
    @GetMapping("/newEvidence")
    public Result test(){
        Gson gson = new Gson();
        BigInteger blockNumber =new BigInteger("0");
        try {
            blockNumber = web3j.getBlockNumber().send().getBlockNumber();
            Object codestr = web3j.getCode("0x0b100988ddc5ff78d6b0f9c066dd1b1fa9e54755").send();
            System.out.println(blockNumber);
            System.out.println("合约信息："+gson.toJson(codestr));
            Object peerInfo = web3j.getPeers().send();
            System.out.println("节点信息："+gson.toJson(peerInfo));
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.OK(blockNumber);
    }



    @ApiOperation("测试调用HelloWorld合约Set")
    @GetMapping("/set")
    public Result testContractSet(String contractAddress,String content){
        if (Objects.isNull(contractAddress)) return Result.error("地址为空");
        HelloWorld helloWorld = HelloWorld.load(contractAddress,web3j,
                credentials,
                new StaticGasProvider(
                        GasConstants.GAS_PRICE, GasConstants.GAS_LIMIT));
        try {
            helloWorld.set(content).send();
        }catch (Exception e){
            log.info("合约调用失败（set）");
            return Result.error("合约调用失败（set）");
        }

        return Result.OK("合约调用成功");
    }

    @ApiOperation("测试调用HelloWorld合约Get")
    @GetMapping("/get")
    public Result testContractGet(String contractAddress){
        if (Objects.isNull(contractAddress) ) return Result.error("地址为空");
        HelloWorld helloWorld = HelloWorld.load(contractAddress,web3j,
                credentials,
                new StaticGasProvider(
                        GasConstants.GAS_PRICE, GasConstants.GAS_LIMIT));
        try {
            return Result.OK(helloWorld.get().send());
        }catch (Exception e){
            log.info("合约调用失败（get）");
        }

        return Result.error("合约调用失败（get）");
    }

}
